package cn.toutatis.common.result;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Gc
 * @date 2019-08-30
 * 通用返回类
 */
public enum ResultStatus implements Serializable {

    //    通用类返回
    UNKNOWN_ERROR(-1, false, "未知错误"),
    SERVICE_NOT_EXIST(100,false,"服务不存在"),
    SUCCESS(200, true, "加载成功"),
    TRANSFER_DATA(300, true, "中转数据"),
    SUCCESS_LOGIN(200, true, "登录成功"),
    SUCCESS_LIST_NULL(2000, false, "请求成功且空数据[LIST]"),
    SUCCESS_OBJECT_NULL(2001, false, "请求成功且空数据[OBJECT]"),
    NULL_SOURCE_ERROR(404, false, "资源不存在"),
    NULL_DATA_ERROR(4040, false, "请求不成功且为空数据"),
    REQUEST_PARAM_NOT_ENOUGH(4050, false, "请求参数不足"),
    REQUEST_PARAM_NOT_ERROR(4060, false, "请求标识错误"),
    //    系统操作类
    SESSION_OUT(400, false, "缓存已失效或未登录"),
    GET_CACHE_INFO(201, true, "获取缓存信息"),
    EXCEPTION_MATCHING(500, false, "异常捕获"),
    ILLEGAL_OPERATION(900, false, "非法操作且行为已记录"),
    //业务类
    HANDLE_SUCCESS(10001, true, "操作成功"),
    HANDLE_ERROR(10002, false, "操作失败"),
    APPLY_SUCCESS(20001, true, "审核成功"),
    APPLY_ERROR(20002, false, "审核失败"),
    UPDATE_SUCCESS(30001, true, "更新成功"),
    UPDATE_ERROR(30002, false, "更新失败"),
    //    用户类
    NOT_LOGIN(505,false,"用户未登录"),
    UN_AUTHORIZED(510, false, "未验证用户"),
    UN_WRITE_AUTH(511, false, "无操作权限"),
    ROLE_NOT_MATCHING(512, false, "未认证"),
    NULL_USER_ACCOUNT(5000, false, "用户不存在"),
    BLANK_PASSWORD(514, false, "密码为空"),
    BLANK_USER_ACCOUNT(515, false, "用户名为空"),
    BANNED_ACCOUNT(516, false, "用户已被封禁"),
    LOG_OUT_SUCCESS(521, true, "注销"),
    PASSWORD_MISTAKE(519, false, "密码错误");

    private Integer code;
    private boolean status;
    private String message;


    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    ResultStatus(Integer code, boolean status, String message) {
        this.code = code;
        this.status = status;
        this.message = message;
    }

    public Map setResponse(Object object) {
        Map<String,Object> responseMap = setResponse();
        responseMap.put("response",object);
        return responseMap;
    }

    /**
     * @param objects 传递额外参数
     * @return map
     */
    public Map setExtra(Object object,HashMap<String,Object> extraParameters){
        HashMap cloneExtraParameters;
        if (extraParameters!= null){
            cloneExtraParameters = (HashMap) extraParameters.clone();
        }else {
            return  setResponse(object);
        }
        Map map =  setResponse(object);
//        response 只许出现一次
        String response = "response";
        boolean containsKey = cloneExtraParameters.containsKey(response);
        if (containsKey){ cloneExtraParameters.remove(response); }
        map.putAll(cloneExtraParameters);
        return map;
    }

    /**
     * @return 没有返回值
     */
    public Map setResponse() {
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("code", this.code);
        responseMap.put("status", this.status);
        responseMap.put("message", this.message);
        responseMap.put("response", null);
        responseMap.put("currentTime",dateFormat.format(new Date()));
        responseMap.put("timestamp",System.currentTimeMillis());
        return responseMap;
    }

    public ResultStatus setMessage(String message) {
        this.message = message;
        return this;
    }


}
